//
//  GameRoom.m
//  Soccer
//
//  Created by Hyun-Woong Kim on 09. 06. 13.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "GameRoom.h"
#import "GameScene.h"
#import "NetworkManager.h"
#import "ClientManager.h"

#define kOffset 5.0

@interface GameRoom ()
@end

@implementation GameRoom


- (id)initWithFrame:(CGRect)frame type:(NSString*)type
{
	if ((self = [super initWithFrame:frame])) 
	{		
		self.opaque = YES;
		self.backgroundColor = [UIColor blackColor];
		
		PlayerJoin = 0;
		PlayerReady = 0;
		
		playerArray = [[NSMutableArray alloc] init];
		
		runningY = kOffset;
		CGFloat width = self.bounds.size.width - 2 * kOffset;
		
		/*
		UIAlertView *alert = [[UIAlertView alloc]	initWithTitle:@"정보 입력"
													message:@"플레이어 이름을 입력하세요"
													delegate:self
													cancelButtonTitle:@"취소"
											  otherButtonTitles:@"확인", nil];
		[alert addTextFieldWithValue:@"" label:@"이름 입력"];

		UITextField *tf = [alert textFieldAtIndex:0];
		tf.clearButtonMode = UITextFieldViewModeWhileEditing;
		tf.keyboardType = UIKeyboardTypeAlphabet;
		tf.keyboardAppearance = UIKeyboardAppearanceAlert;
		tf.autocapitalizationType = UITextAutocapitalizationTypeWords;
		tf.autocorrectionType = UITextAutocorrectionTypeNo;
		
		[alert show];
		*/ 
		
		UIImageView* img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gameroom.png"]];
		[self addSubview:img];
		[img release];
		
		UILabel* label = [[UILabel alloc] initWithFrame:CGRectZero];
		[label setTextAlignment:UITextAlignmentCenter];
		[label setFont:[UIFont boldSystemFontOfSize:15.0]];
		[label setTextColor:[UIColor whiteColor]];
		[label setShadowColor:[UIColor colorWithWhite:0.0 alpha:0.75]];
		[label setShadowOffset:CGSizeMake(1,1)];
		[label setBackgroundColor:[UIColor clearColor]];
		label.text = @"All-m iPhone Soccer";
		label.numberOfLines = 1;
		[label sizeToFit];
		label.frame = CGRectMake(kOffset, runningY, width, label.frame.size.height);
		[self addSubview:label];
		
		runningY += label.bounds.size.height;
		[label release];
		
		label = [[UILabel alloc] initWithFrame:CGRectZero];
		[label setTextAlignment:UITextAlignmentCenter];
		[label setFont:[UIFont boldSystemFontOfSize:24.0]];
		[label setTextColor:[UIColor whiteColor]];
		[label setShadowColor:[UIColor colorWithWhite:0.0 alpha:0.75]];
		[label setShadowOffset:CGSizeMake(1,1)];
		[label setBackgroundColor:[UIColor clearColor]];
		label.text = @"Game Room";
		label.numberOfLines = 1;
		[label sizeToFit];
		label.frame = CGRectMake(kOffset, runningY, width, label.frame.size.height);
		[self addSubview:label];
		
		runningY += label.bounds.size.height + 6.0f;
		[label release];
		
		label = [[UILabel alloc] initWithFrame:CGRectZero];
		[label setTextAlignment:UITextAlignmentCenter];
		[label setFont:[UIFont boldSystemFontOfSize:15.0]];
		[label setTextColor:[UIColor whiteColor]];
		[label setShadowColor:[UIColor colorWithWhite:0.0 alpha:0.75]];
		[label setShadowOffset:CGSizeMake(1,1)];
		[label setBackgroundColor:[UIColor clearColor]];
		label.text = @"Waiting for other players";
		label.numberOfLines = 1;
		[label sizeToFit];
		label.frame = CGRectMake(kOffset, runningY, width, label.frame.size.height);
		[self addSubview:label];
		
		button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
		[button setFrame:CGRectMake(kOffset, runningY, 120.0f, 40.0f)];
		[button setCenter:CGPointMake(240.0f, 280.0f)];
		[button setTitle:@"Start" forState:UIControlStateNormal];
		[button addTarget:self action:@selector(startGame) forControlEvents:UIControlEventTouchUpInside];
		[self addSubview:button];
		
//		UIButton* backButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//		[backButton setFrame:CGRectMake(kOffset, runningY, 40.0f, 40.0f)];
//		[backButton setCenter:CGPointMake(40.0f, 280.0f)];
//		[backButton setTitle:@"Back" forState:UIControlStateNormal];
//		[backButton addTarget:self action:@selector(startGame) forControlEvents:UIControlEventTouchUpInside];
//		[self addSubview:backButton];
//		[backButton release];
		
	}
	return self;
}

- (void)dealloc
{
	// Cleanup any running resolve and free memory
//	[button release];
	[super dealloc];
}

- (void) removeButton
{
	[button removeFromSuperview];
}


- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
	printf("User Pressed Button %d\n", buttonIndex + 1);
//	printf("Text Field 1: %s\n", [[[modalView textFieldAtIndex:0] text] cStringUsingEncoding:1]);
	[alertView release];
}
 

- (void) InsertPlayer:(NSString *)name ofId:(int)player_id
{
	UILabel *thisPlayer = [[UILabel alloc] initWithFrame:CGRectZero];
	[thisPlayer setFont:[UIFont boldSystemFontOfSize:15.0]];
	[thisPlayer setTextColor:[UIColor blackColor]];
	[thisPlayer setBackgroundColor:[UIColor clearColor]];
	thisPlayer.text = name;
	thisPlayer.numberOfLines = 1;
	[thisPlayer sizeToFit];
	
	if(player_id % 2 == 0)
	{
		thisPlayer.frame = CGRectMake(40.0f, 140.0f + 36.0f * numberOfTeamA, thisPlayer.frame.size.width, thisPlayer.frame.size.height);
		numberOfTeamA++;
	}
	else
	{
		thisPlayer.frame = CGRectMake(280.0f, 140.0f + 36.0f * numberOfTeamB, thisPlayer.frame.size.width, thisPlayer.frame.size.height);
		numberOfTeamB++;
	}
	
	[playerArray insertObject:thisPlayer atIndex:player_id];
	[self addSubview:thisPlayer];
}

- (void) DeletePlayer:(int)player_id
{
	UILabel *thisPlayer = [playerArray objectAtIndex:player_id];
	[thisPlayer removeFromSuperview];
	[thisPlayer release];
}


- (void)startGame
{
	[[NetworkManager sharedNetworkManager] startGame];
}

@end
